using UnityEngine;

public class LightingController : MonoBehaviour
{
    [Header("灯光引用")]
    public Light mainDirectionalLight; // 场景中的主直射光
    
    [Header("旋转设置")]
    public float rotationSpeed = 40.0f; // 旋转速度 (度/秒)
    private bool isRotating = false; // 当前是否正在旋转
    private int rotateDirection = 0; // 1=正向, -1=反向, 0=不转
    
    [Header("旋转轴")]
    public bool rotateX = false; // 是否绕X轴旋转
    public bool rotateY = true;  // 是否绕Y轴旋转
    
    // 记录初始状态
    private Vector3 initialRotation;
    
    void Start()
    {
        // 如果未指定灯光，尝试找场景中的主灯光
        if (mainDirectionalLight == null)
        {
            // 尝试获取场景中的所有灯光
            Light[] lights = FindObjectsOfType<Light>();
            foreach (Light light in lights)
            {
                // 找到第一个类型为直射光的灯光
                if (light.type == LightType.Directional)
                {
                    mainDirectionalLight = light;
                    break;
                }
            }
        }
        
        if (mainDirectionalLight == null)
        {
            Debug.LogWarning("LightingController: 未找到直射光源!");
            enabled = false;
            return;
        }
        
        // 记录初始旋转值
        initialRotation = mainDirectionalLight.transform.rotation.eulerAngles;
    }
    
    void Update()
    {
        // 持续旋转
        if (rotateDirection != 0 && mainDirectionalLight != null)
        {
            Vector3 rotation = Vector3.zero;
            if (rotateX)
                rotation.x = rotationSpeed * rotateDirection * Time.deltaTime;
            if (rotateY)
                rotation.y = rotationSpeed * rotateDirection * Time.deltaTime;
            mainDirectionalLight.transform.Rotate(rotation, Space.World);
        }
        // 保留原有自动旋转功能
        else if (isRotating && mainDirectionalLight != null)
        {
            Vector3 rotation = Vector3.zero;
            
            if (rotateX)
                rotation.x = rotationSpeed * Time.deltaTime;
                
            if (rotateY)
                rotation.y = rotationSpeed * Time.deltaTime;
                
            mainDirectionalLight.transform.Rotate(rotation, Space.World);
        }
    }
    
    /// <summary>
    /// 切换是否自动旋转
    /// </summary>
    public void ToggleRotation()
    {
        isRotating = !isRotating;
    }
    
    /// <summary>
    /// 设置旋转状态
    /// </summary>
    public void SetRotation(bool rotate)
    {
        isRotating = rotate;
    }
    
    /// <summary>
    /// 重置灯光方向到初始状态
    /// </summary>
    public void ResetLightDirection()
    {
        if (mainDirectionalLight != null)
        {
            mainDirectionalLight.transform.rotation = Quaternion.Euler(initialRotation);
            isRotating = false;
        }
    }
    
    /// <summary>
    /// 手动旋转特定角度
    /// </summary>
    public void RotateLight(float xAngle, float yAngle)
    {
        if (mainDirectionalLight != null)
        {
            Vector3 rotation = Vector3.zero;
            
            if (rotateX)
                rotation.x = xAngle;
                
            if (rotateY)
                rotation.y = yAngle;
                
            mainDirectionalLight.transform.Rotate(rotation, Space.World);
        }
    }
    
    /// <summary>
    /// 切换旋转轴
    /// </summary>
    public void ToggleRotationAxis(bool useX, bool useY)
    {
        rotateX = useX;
        rotateY = useY;
    }
    
    /// <summary>
    /// 长按开始旋转（direction=1为正向，-1为反向）
    /// </summary>
    public void StartRotate(int direction)
    {
        rotateDirection = Mathf.Clamp(direction, -1, 1);
    }
    
    /// <summary>
    /// 停止旋转
    /// </summary>
    public void StopRotate()
    {
        rotateDirection = 0;
    }
}